package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Eventqualitytype;
import cn.edu.scau.cmi.ema.domain.Qualitytype;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("EventqualitytypeController")
public class EventqualitytypeController extends CmiEmaController {

	@RequestMapping("/listEventqualitytypes")
	public ModelAndView listEventqualitytypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = eventqualitytypePaginationService.updatePagination(request, pagination);
		Set<Eventqualitytype> eventqualitytypes = eventqualitytypePaginationService
				.getPaginationEventqualitytypes(pagination);

		mav.addObject("eventqualitytypes", eventqualitytypes);
		mav.addObject("entityName", "eventqualitytype");
		mav.addObject("pagination", pagination);
		mav.setViewName("eventqualitytype/listEventqualitytypes");
		return mav;
	}

	public String listEventqualitytypes() {
		return "redirect:/listEventqualitytypes";
	}

	@RequestMapping("/viewEventqualitytype")
	public ModelAndView viewEventqualitytype(@RequestParam Integer eventqualitytypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("eventqualitytype", eventqualitytypeDAO.findEventqualitytypeByPrimaryKey(eventqualitytypeId));
		mav.setViewName("eventqualitytype/viewEventqualitytype");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEventqualitytypes")
	public ModelAndView confirmDeleteEventqualitytypes(
			@RequestParam(value = "deleteEventqualitytypesIds", required = true) Set<Integer> deleteEventqualitytypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Eventqualitytype> deleteEventqualitytypes = eventqualitytypeDAO
				.findEventqualitytypesByIds(deleteEventqualitytypesIds);
		Set<Eventqualitytype> referencedEventqualitytypes = eventqualitytypeService
				.loadReferenceEventqualitytypes(deleteEventqualitytypes);
		boolean hasReferenced;
		if (referencedEventqualitytypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteEventqualitytypes);
			hasReferenced = false;
		} else {
			deleteEventqualitytypes.removeAll(referencedEventqualitytypes);
			mav.addObject("referencedEntitys", referencedEventqualitytypes);
			mav.addObject("deleteEntitys", deleteEventqualitytypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Eventqualitytype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEventqualitytypes");
		return mav;
	}

	@RequestMapping("/deleteEventqualitytype")
	public String deleteEventqualitytype(@RequestParam Integer eventqualitytypeId) {
		Eventqualitytype eventqualitytype = eventqualitytypeDAO.findEventqualitytypeByPrimaryKey(eventqualitytypeId);
		eventqualitytypeService.deleteEventqualitytype(eventqualitytype);
		return "forward:/listEventqualitytypes";
	}

	@RequestMapping("/confirmDeleteEventqualitytype")
	public ModelAndView confirmDeleteEventqualitytype(@RequestParam Integer eventqualitytypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("eventqualitytype", eventqualitytypeDAO.findEventqualitytypeByPrimaryKey(eventqualitytypeId));

		mav.setViewName("eventqualitytype/deleteEventqualitytype");

		return mav;
	}

	@RequestMapping("/deleteEventqualitytypes")
	public String deleteEventqualitytypes(
			@RequestParam(value = "deleteEventqualitytypesId", required = true) Set<Integer> deleteEventqualitytypesId) {
		eventqualitytypeService.deleteEventqualitytypes(deleteEventqualitytypesId);
		return "forward:/listEventqualitytypes";
	}

	@RequestMapping("/editEventqualitytype")
	public ModelAndView editEventqualitytype(@RequestParam Integer eventqualitytypeId) {

		ModelAndView mav = new ModelAndView();

		Eventqualitytype eventqualitytype = eventqualitytypeDAO.findEventqualitytypeByPrimaryKey(eventqualitytypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("eventqualitytype", eventqualitytype);

		// 把所有的引用eventqualitytype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(eventqualitytypeService.loadEventqualitytypeObjectReferencedSet(eventqualitytype));

		// 将编辑页面eventqualitytype添加到mav中
		mav.setViewName("eventqualitytype/editEventqualitytype");
		return mav;
	}

	@RequestMapping("/newEventqualitytype")
	public ModelAndView newEventqualitytype() {
		ModelAndView mav = new ModelAndView();
		Eventqualitytype eventqualitytype = new Eventqualitytype();

		mav.addObject("eventqualitytype", eventqualitytype);

		// 把所有的引用eventqualitytype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(eventqualitytypeService.loadEventqualitytypeObjectReferencedSet(eventqualitytype));

		mav.addObject("newFlag", true);
		mav.setViewName("eventqualitytype/editEventqualitytype");

		return mav;
	}

	@RequestMapping("/saveEventqualitytype")
	public ModelAndView saveEventqualitytype(@ModelAttribute Eventqualitytype eventqualitytype) {

		ModelAndView mav = new ModelAndView();

		Event eventTemp = eventqualitytype.getEvent();
		Event event = eventTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(eventDAO.findEventById(eventTemp.getId()));
		Qualitytype qualitytypeTemp = eventqualitytype.getQualitytype();
		Qualitytype qualitytype = qualitytypeTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(qualitytypeDAO.findQualitytypeById(qualitytypeTemp.getId()));

		Eventqualitytype existingEventqualitytype = CmiHibernateProxyUtil
				.deproxy(eventqualitytypeDAO.findEventqualitytypeByPhysicalPrimaryKey(event.getId(),
						qualitytype.getId(), eventqualitytype.getDegree()));

		eventqualitytype.setEvent(event);
		eventqualitytype.setQualitytype(qualitytype);
		// 新建记录
		if (eventqualitytype.getId() == null) {
			if (eventqualitytype.equals(existingEventqualitytype)) {

				mav.addAllObjects(eventqualitytypeService.loadEventqualitytypeClassReferencedSet(eventqualitytype));
				// mav = editContinue(mav, eventqualitytype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("eventqualitytype/editEventqualitytype");
			} else {
				eventqualitytypeService.saveEventqualitytype(eventqualitytype);
				mav.setViewName("forward:/listEventqualitytypes");
			}
		}
		// 编辑记录
		else {
			Eventqualitytype originEventqualitytype = CmiHibernateProxyUtil
					.deproxy(eventqualitytypeDAO.findEventqualitytypeByPrimaryKey(eventqualitytype.getId()));
			if (!eventqualitytype.equals(originEventqualitytype) && eventqualitytype.equals(existingEventqualitytype)) {
				mav.addAllObjects(eventqualitytypeService.loadEventqualitytypeClassReferencedSet(eventqualitytype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("eventqualitytype/editEventqualitytype");
			} else {
				eventqualitytypeService.saveEventqualitytype(eventqualitytype);
				mav.setViewName("forward:/listEventqualitytypes");
			}
		}
		return mav;
	}

	@RequestMapping("/eventqualitytypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
